
import { reqGoodsInfo ,reqAddOrUpdateShopCart} from "@/api"
import { getUUID } from "@/utils/uuid_token"



const state = {
    goodInfo:{},
    //游客的临时身份uuid
    uuid_token:getUUID(),
}
const mutations = {
    GETGOODINFO(state,goodInfo) {
        state.goodInfo = goodInfo
    }
}
const actions = {
    async getGoodInfo ({commit},skuId){
        let result = await reqGoodsInfo(skuId);
        if(result.code==200){
            commit('GETGOODINFO',result.data)
        }
    },
     //加入购物车产品||或者更新某一个产品的个数接口
     async addOrUpdateShopCart({commit},{skuId,skuNum}){
        let result  = await reqAddOrUpdateShopCart(skuId,skuNum);
        console.log(result)
        //加入购物车成功
        if(result.code==200){
            //代表当前函数addShopCart返回的promise状态是成功
            return 'ok';
        }else{
            return Promise.reject(new Error('faile'));
        }
        //一个函数执行，默认返回的是undefined，代表promise成功，
        //函数执行默认返回的是undefined，永远是成功，什么时候undefined不起作用了
    }
    

}
const getters = {
    categoryView(state){
        // goodInfo是一个空对象，空对象打点categoryView等于undefined，你把undefined给组件会造成假报错，因此或一个空对象，有一个默认值
        return state.goodInfo.categoryView || {};
    },
    //产品信息的数据
    skuInfo(){
        return state.goodInfo.skuInfo || {};
    },
    spuSaleAttrList(state){
         

        return state.goodInfo.spuSaleAttrList || [];
        
    }
    


}

export default {
    state,
    mutations,
    actions,
    getters
}